//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Management;
using Microsoft.BizTalk.Operations;

namespace GML.BizTalk.OperationManager.Library
{
    public class ServiceInstances
    {
        public ServiceInstances()
        {
        }

        public static bool StartAllAdminSuspendedInstances(string btsDbServerName, string btsMgntDbName, out Exception ex)
        {
            BizTalkOperations btsOps = null;
            Instance instance = null;
            try
            {
                btsOps = new BizTalkOperations(btsDbServerName, btsMgntDbName);

                IEnumerator ie = btsOps.GetServiceInstances().GetEnumerator();
                while (ie.MoveNext())
                {
                    instance = (Instance)ie.Current;
                    if (instance.InstanceStatus.Equals(InstanceStatus.Suspended)
                        &&
                        instance.ErrorDescription.Trim().Equals("This service instance was suspended by a BizTalk administrator.")
                        )
                    {
                        System.Diagnostics.Debug.WriteLine("-----------------------------------------------------");
                        System.Diagnostics.Debug.WriteLine("instance.ID.ToString() = " + instance.ID.ToString());
                        System.Diagnostics.Debug.WriteLine("instance.CreationTime.ToString() = " + instance.CreationTime.ToString());
                        System.Diagnostics.Debug.WriteLine("instance.ErrorDescription.ToString() = " + instance.ErrorDescription);
                        System.Diagnostics.Debug.WriteLine("instance.HostName.ToString() = " + instance.HostName);
                        System.Diagnostics.Debug.WriteLine("instance.InstanceStatus.ToString() = " + instance.InstanceStatus.ToString());
                        System.Diagnostics.Debug.WriteLine("instance.ServiceType.ToString() = " + instance.ServiceType);
                        System.Diagnostics.Debug.WriteLine("instance.Class.ToString() = " + instance.Class.ToString());

                        Logger.doLog(Guid.NewGuid().ToString(), "-----------------------------------------------------", Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.ID.ToString() = " + instance.ID.ToString(), Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.CreationTime.ToString() = " + instance.CreationTime.ToString(), Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.ErrorDescription.ToString() = " + instance.ErrorDescription, Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.HostName.ToString() = " + instance.HostName, Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.InstanceStatus.ToString() = " + instance.InstanceStatus.ToString(), Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "instance.ServiceType.ToString() = " + instance.ServiceType, Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(),"instance.Class.ToString() = " + instance.Class.ToString(),Logger.ServerityLevel.INFO);
                        
                        btsOps.ResumeInstance(instance.ID);

                        System.Diagnostics.Debug.WriteLine("instance being resume: " + instance.ID.ToString());
                        System.Diagnostics.Debug.WriteLine("-----------------------------------------------------");
                        Logger.doLog(Guid.NewGuid().ToString(), "instance being resume: " + instance.ID.ToString(), Logger.ServerityLevel.INFO);
                        Logger.doLog(Guid.NewGuid().ToString(), "-----------------------------------------------------", Logger.ServerityLevel.INFO);
                    }
                }
            }
            catch (Exception e)
            {
                ex = e;
                return false;
            }
            finally
            {
                btsOps = null;
                instance = null;
            }
            ex = null;
            return true;
        }
    }
}
